package cn.exrick.xboot.modules.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

/**
 * Created by cailiang on 2020/5/29.
 */
public class GPSDistance {
    final static double EARTH_RADIUS = 6378.137;

    final static double rad(double d) {
        return d * Math.PI / 180.0;
    }


    public static int couuntGps(String gps) {
        JSONArray arr = JSON.parseArray(gps);
        double distance = 0;
        for (int i = 0; i < arr.size() - 1;i ++){
            distance += GPSDistance.distance(arr.getJSONArray(i + 1).getDouble(1),arr.getJSONArray(i + 1).getDouble(0),arr.getJSONArray(i).getDouble(1),arr.getJSONArray(i).getDouble(0));
        }
        int numPatrolCourse = (int)distance;
        return numPatrolCourse;
    }



    /**
     * 计算两个GPS点之间的距离
     * @param lat1 纬度,-90 ~ 90,
     * @param lng1 精度,-180 ~ 180
     * @param lat2
     * @param lng2
     * @return
     */
    public static double distance(double lat1, double lng1, double lat2, double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        // 公里转米
        s = Math.round(s * 10000) / 10.0f;
        return s;
    }

    public static void main(String[] args) {
        String pos = "[[116.77558675130209,40.089755859375],[116.77558756510416,40.089755859375],[116.77558702256944,40.089755859375],[116.77558539496528,40.089756401909725],[116.77558512369792,40.089756944444446],[116.77558430989583,40.08975802951389],[116.7755845811632,40.08975802951389],[116.77558539496528,40.08975830078125],[116.77561008029514,40.08970594618056],[116.77564887152778,40.08965223524306],[116.77562228732639,40.089609375],[116.77564127604167,40.08951171875],[116.77563856336806,40.089420030381945],[116.77566107855903,40.08931098090278],[116.77567843967014,40.08922661675347],[116.77567952473959,40.089105631510414],[116.77570041232639,40.08898980034722],[116.77568115234375,40.08886121961805],[116.7756849500868,40.08873806423611],[116.77569254557292,40.08860921223958],[116.77569715711806,40.08844943576389],[116.77569552951388,40.088284233940975],[116.77571316189236,40.08815863715278],[116.77571750217014,40.088039008246525],[116.77570556640624,40.087900390625],[116.77569010416667,40.08778049045139],[116.77570393880208,40.08766981336805],[116.77563937717014,40.0875732421875],[116.77561414930555,40.08746473524305],[116.77568603515626,40.08728325737847],[116.77571858723958,40.087041015625],[116.77571858723958,40.087041015625],[116.77571858723958,40.087041015625],[116.77571858723958,40.087041015625],[116.77571858723958,40.087041015625],[116.77571858723958,40.087041015625],[116.77561442057292,40.085362413194446],[116.77569200303819,40.08538899739583],[116.77571668836805,40.08528971354167],[116.77576633029514,40.085179036458335],[116.77571316189236,40.08510091145833],[116.77579427083333,40.08502468532986],[116.77583875868055,40.085],[116.77593804253472,40.08498209635417],[116.77603217230903,40.08497477213542],[116.77611599392361,40.08496934678819],[116.77621988932292,40.08494520399306],[116.77632595486111,40.084931640625],[116.77639133029514,40.084931640625],[116.7764564344618,40.084933268229165],[116.77651340060764,40.08493001302083],[116.77656358506944,40.08492350260417],[116.77663330078126,40.08490912543403],[116.77672173394097,40.08489854600695],[116.77680148654514,40.0848876953125],[116.77688802083334,40.084880913628474],[116.77697672526041,40.084865993923614],[116.77707058376735,40.08484890407986],[116.77716227213541,40.08483262803819],[116.77725233289931,40.08481608072917],[116.77734809027778,40.084803873697915],[116.77745659722223,40.08479871961806],[116.77756103515625,40.0847837999132],[116.77767930772569,40.084758029513885],[116.77778401692709,40.08475287543403],[116.77788465711805,40.084750705295136],[116.77799099392361,40.08474745008681],[116.77810139973958,40.08474175347222],[116.77820475260417,40.084729817708336],[116.77829698350695,40.084722493489586],[116.77837782118056,40.084722493489586],[116.7784450954861,40.08473090277778],[116.77851752387153,40.08474663628472],[116.77861328125,40.08475830078125],[116.77871012369792,40.084773763020834],[116.77881456163195,40.084782172309026],[116.77892469618055,40.08479112413195],[116.77902560763889,40.08479953342014],[116.77912027994792,40.08480848524306],[116.77917643229166,40.08482177734375],[116.77920328776041,40.08484619140625],[116.779208984375,40.084881727430556],[116.77918565538195,40.08493896484375],[116.77915445963542,40.08499891493056],[116.77912407769097,40.085055067274304],[116.77911566840278,40.085103352864586],[116.77910861545139,40.08514350043403],[116.77909857855903,40.085171712239585],[116.77909152560764,40.08519802517361],[116.77908365885416,40.08522623697917],[116.77907823350695,40.085253363715275],[116.77908284505209,40.085293511284725],[116.77908420138888,40.085331488715276],[116.77908311631944,40.085370008680556],[116.77908311631944,40.0854115125868],[116.77907470703126,40.0854638671875],[116.77906684027778,40.08551513671875],[116.77906032986111,40.08555718315972],[116.77905056423612,40.08560031467014],[116.77903835720485,40.085658637152775],[116.77903727213541,40.085713161892365],[116.77904052734375,40.08575927734375],[116.77904541015624,40.08579915364583],[116.77904405381945,40.08584364149306],[116.77903998480903,40.085895724826386],[116.77904324001736,40.08594428168403],[116.77904676649305,40.08599745008681],[116.77903645833334,40.086055501302084],[116.77902045355903,40.08610866970486],[116.77901448567708,40.086154242621525],[116.77900634765625,40.0861949327257],[116.77899359809028,40.08624321831597],[116.77899332682291,40.08629611545139],[116.77900417751736,40.0863427734375],[116.77901611328124,40.0863880750868],[116.77902316623263,40.08643364800347],[116.7790150282118,40.08649197048611],[116.77900716145834,40.08653862847222],[116.77900689019097,40.08657606336806],[116.77902018229166,40.08662353515625],[116.7790416124132,40.08667507595486],[116.77904893663194,40.08673122829861],[116.7790407986111,40.086786295572914],[116.77902777777778,40.08684814453125],[116.77900119357639,40.0869127061632],[116.77898410373264,40.08696804470486],[116.77897677951388,40.08700737847222],[116.77896484375,40.08703803168403],[116.77895182291667,40.087077365451385],[116.77894314236111,40.087123209635415],[116.77893771701389,40.087178548177086],[116.77893636067708,40.087237684461805],[116.77893527560764,40.08728759765625],[116.77892849392362,40.08733642578125],[116.7789208984375,40.08738525390625],[116.77891899956597,40.08743381076389],[116.77891845703125,40.08749240451389],[116.7789133029514,40.08754638671875],[116.77890570746528,40.08759494357639],[116.7789048936632,40.08764051649305],[116.7789076063368,40.0876841905382],[116.77891086154514,40.08773030598958],[116.77891140407986,40.087781575520836],[116.77891004774305,40.08782850477431],[116.77890516493055,40.08787868923611],[116.77889458550347,40.08793701171875],[116.7788804796007,40.08799180772569],[116.77886935763888,40.08803955078125],[116.7788623046875,40.08808213975694],[116.77885498046875,40.08813286675347],[116.77881076388888,40.088251953125],[116.77883572048611,40.08819064670139],[116.77879584418403,40.08833224826389],[116.77878607855902,40.088406032986114],[116.77877360026042,40.08847086588542],[116.77876871744792,40.08853515625],[116.7787765842014,40.08859076605903],[116.77877848307291,40.08863715277778],[116.7787828233507,40.088678656684024],[116.77878255208333,40.08872748480903],[116.7787757703993,40.08878228081597],[116.77877685546875,40.088841145833335],[116.77878879123264,40.088885633680555],[116.77879964192708,40.088922797309024],[116.77880181206598,40.08895887586806],[116.77881266276042,40.08899197048611],[116.77883327907986,40.08907958984375],[116.7788259548611,40.08903347439236],[116.77884331597222,40.08912055121528],[116.77884847005208,40.08916341145834],[116.77885118272569,40.08920166015625],[116.77884982638889,40.089244520399305],[116.7788490125868,40.089298502604166],[116.77884684244792,40.089359537760416],[116.77884087456597,40.089411078559024],[116.77883572048611,40.089458550347224],[116.77882839626736,40.08950981987847],[116.77881374782986,40.08955186631945],[116.77880479600694,40.08957628038195],[116.7787966579861,40.08960232204861],[116.77879503038194,40.0896373155382],[116.77880615234375,40.08968587239583],[116.77881076388888,40.08973605685764],[116.77880832248263,40.08978271484375],[116.77880099826389,40.089838324652774],[116.77879258897569,40.089891764322914],[116.77878580729167,40.089939507378475],[116.77878309461805,40.08998345269097],[116.77877766927084,40.09002387152778],[116.77877034505208,40.09006998697917],[116.77876573350694,40.090121527777775],[116.77875813802083,40.09017415364583],[116.77875325520833,40.090228407118055],[116.77874945746528,40.09027994791666],[116.77874945746528,40.09033094618056],[116.77875244140625,40.09037624782986],[116.77874430338542,40.09042561848958],[116.77873589409722,40.090474446614586],[116.77873101128472,40.0905238172743],[116.77872639973958,40.09056857638889],[116.7787193467882,40.09060628255208],[116.77871365017361,40.090640462239584],[116.77870849609376,40.090678168402775],[116.77870415581597,40.09070638020833],[116.77870035807291,40.09073649088542],[116.77869737413195,40.0907679578993],[116.77869737413195,40.09079644097222],[116.7786976453993,40.09081787109375],[116.7786984592014,40.09085096571181],[116.77869466145833,40.090892198350694],[116.7786865234375,40.09093207465278],[116.77868598090278,40.09098036024306],[116.7786949327257,40.09103190104167],[116.77869249131945,40.09107964409722],[116.77868543836806,40.09112820095486],[116.77868245442708,40.09117621527778],[116.77868055555555,40.09122287326389],[116.7786767578125,40.09126844618056],[116.77867296006944,40.09131700303819],[116.77866455078124,40.09135959201389],[116.7786572265625,40.091395670572915],[116.7786488172743,40.09142361111111],[116.77863959418403,40.091441786024305],[116.77863715277778,40.091464029947915],[116.77864312065972,40.0915003797743],[116.77864800347223,40.09151394314236],[116.77866861979167,40.09153862847222],[116.77868977864583,40.09158474392361],[116.77868462456597,40.09163764105903],[116.77867268880209,40.09167697482639],[116.77864990234374,40.09171088324653],[116.77862928602431,40.09172770182292],[116.77860541449652,40.09174913194445],[116.77859673394097,40.09177951388889],[116.77858805338542,40.09181070963542],[116.77857231987848,40.091844889322914],[116.77856336805556,40.09186821831597],[116.77855305989583,40.09189778645833],[116.77854953342013,40.09194010416667],[116.77856825086806,40.091959364149304],[116.77858723958333,40.0919740125868],[116.7785853407118,40.09198920355903],[116.77857801649306,40.091998155381944],[116.77857855902778,40.09201117621528],[116.77859185112847,40.092030164930556],[116.77860270182292,40.09205485026042],[116.77861300998264,40.09207926432292],[116.77860460069445,40.09209364149306],[116.7785972764757,40.092096354166664],[116.77859619140625,40.092120225694444],[116.77859456380209,40.09215386284722],[116.77858832465277,40.09220160590278],[116.77857828776041,40.09224527994792],[116.77856526692709,40.092294921875],[116.77856689453125,40.09234673394097],[116.77856662326388,40.09239312065972],[116.77856662326388,40.09243869357639],[116.77856608072916,40.0924853515625],[116.77856608072916,40.0925306532118],[116.77856146918403,40.09258327907986],[116.77856309678819,40.09262722439236],[116.77856580946181,40.09266764322917],[116.7785671657986,40.09271701388889],[116.77856906467014,40.09277235243056],[116.77856635199653,40.09282524956597],[116.77855116102431,40.092875434027775],[116.77852973090278,40.09292778862847],[116.77852132161458,40.092960069444445],[116.77852186414931,40.09298936631944],[116.77853461371528,40.09303005642361],[116.77854899088541,40.093066948784724],[116.7785400390625,40.093111979166665],[116.77854085286458,40.09315375434028],[116.77853895399305,40.09320014105903],[116.77852756076389,40.0932470703125],[116.77851752387153,40.09327880859375],[116.77849012586806,40.09336181640625],[116.77837836371528,40.09350396050347],[116.77836100260417,40.09353000217014],[116.77836100260417,40.09353000217014],[116.77836263020833,40.09356065538194],[116.77838650173611,40.09359510633681],[116.7784033203125,40.0936181640625],[116.77842909071181,40.09367241753472],[116.77844401041666,40.093724229600696],[116.77845241970486,40.09376220703125],[116.77845947265625,40.0938170030382],[116.77846923828125,40.093868272569445],[116.77848036024305,40.09391167534722],[116.77848795572916,40.093951822916665],[116.77848605685764,40.09401177300347],[116.77848497178819,40.0940638563368],[116.77848958333334,40.094111328125],[116.77848415798611,40.094173719618055],[116.77847629123264,40.09425184461806],[116.77847629123264,40.09430881076389],[116.7784752061632,40.09435139973959],[116.77847276475694,40.09438856336806],[116.77847222222222,40.09441460503472],[116.77847086588541,40.094456922743056],[116.7784632703993,40.09449734157986],[116.7784521484375,40.094547526041666],[116.7784535047743,40.0945947265625],[116.77845594618056,40.094646267361114],[116.77846462673611,40.09469753689236],[116.77846408420139,40.09475070529514],[116.77845323350695,40.094813910590275],[116.77844265407987,40.09485568576389],[116.77841227213541,40.094913194444445],[116.77839246961805,40.09496771918403],[116.77840033637153,40.0950230577257],[116.77840901692709,40.09508463541667],[116.77840359157986,40.09515570746528],[116.77840549045139,40.09522677951389],[116.77841118706597,40.0952845594618],[116.77841281467013,40.09534342447917],[116.77841769748264,40.09540771484375],[116.77841634114583,40.09549045138889],[116.77842447916667,40.09556613498264],[116.77843180338542,40.09564371744791],[116.77844699435764,40.095715874565975],[116.77845567491319,40.095789930555554],[116.7784521484375,40.095859917534725],[116.77843207465278,40.095935601128474],[116.7784130859375,40.09601128472222],[116.77841362847222,40.09609212239583],[116.77841362847222,40.09609212239583],[116.77841362847222,40.09609212239583],[116.77841362847222,40.09609212239583],[116.77841362847222,40.09609212239583],[116.77841362847222,40.09609212239583],[116.77841362847222,40.09609212239583],[116.77678114149306,40.09279106987847],[116.77672037760416,40.092814127604164],[116.77640462239583,40.09274142795139],[116.77622748480903,40.09268391927083],[116.77612413194444,40.09265245225694],[116.77600043402778,40.09260796440972],[116.7759339735243,40.092577582465275],[116.77590386284722,40.09256022135417],[116.77590115017361,40.09255859375],[116.77588351779514,40.09255099826389],[116.7758677842882,40.09254448784722],[116.77584174262152,40.09253662109375],[116.7758034939236,40.09252658420139],[116.77577473958333,40.09251925998264],[116.77576253255208,40.0925160047743],[116.7757595486111,40.09251573350694],[116.77575358072917,40.09251274956597],[116.77574951171874,40.09251139322917],[116.7757484266493,40.092511121961806],[116.7757484266493,40.092511121961806],[116.77570285373264,40.09247802734375],[116.77565158420138,40.09243191189236],[116.7756298828125,40.09241373697917]]";
        int a = GPSDistance.couuntGps(pos);
        String str = a+".00";
//        JSONArray arr = JSON.parseArray(pos);
//        double distance = 0;
//        for (int i = 0; i < arr.size() - 1;i ++){
//            distance += GPSDistance.distance(arr.getJSONArray(i + 1).getDouble(1),arr.getJSONArray(i + 1).getDouble(0),arr.getJSONArray(i).getDouble(1),arr.getJSONArray(i).getDouble(0));
//        }
        System.out.println(str);
    }
}
